# -*- coding:utf-8 -*-
class Solution:
    def NumberOf1(self, n):
        # 补码的求法：先求原码，符号位不变求反码，再对反码加1即为补码（负数的符号位为1，在求反码补码的过程中符号位不变）
        # write code here
        res = [0 for _ in range(32)]
        if n < 0:
            res[0] = 1
        abs_n = abs(n)
        index = len(res) - 1
        while abs_n // 2 != 0:
            if abs_n % 2:
                res[index] = 1
            index -= 1
            abs_n = abs_n // 2
        if index:
            res[index] = abs_n

        if not res[0]:
            return sum(res)
        res = [abs(item - 1) for item in res]
        res[0] = 1

        i = len(res) - 1
        while True:
            if res[i] == 0:
                res[i] += 1
                break
            else:
                res[i] = 0
                i -= 1

        return sum(res)

a = Solution()
a.NumberOf1(-1)